import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-07-28
 * Time:17:57
 * URL
 * 方法
 * 版本号
 * query string
 * context path
 * 所有的 header
 */
@WebServlet("/showRequest")
public class HttpServletRequest extends HttpServlet {
    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("URL:");
        stringBuilder.append(req.getRequestURI());
        stringBuilder.append("<br>");
        stringBuilder.append("方法:"+req.getMethod());
        stringBuilder.append("<br>");
        stringBuilder.append("版本号："+req.getProtocol());
        stringBuilder.append("<br>");
        stringBuilder.append("query string:"+req.getQueryString());
        stringBuilder.append("<br>");
        stringBuilder.append("context path"+req.getContextPath());
        stringBuilder.append("<br>");
        stringBuilder.append("所有的 header:");
        stringBuilder.append("<br>");
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String value = req.getHeader(name);
            stringBuilder.append(name + ":" + value + "<br>");
        }
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write(stringBuilder.toString());
    }
}
